package com.duoo.pag.pag.ninepatch

import NinePatchFrameLayout
import android.content.Context
import android.view.View
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView

class NinePatchView(
    context: Context,
    messenger: BinaryMessenger,
    id: Int,
    creationParams: Map<String, Any>?,
) :
    PlatformView {


    private val view = NinePatchFrameLayout(context)
    private val channel = MethodChannel(messenger, "nine_patch_flutter_view_$id")

    init {
        val url = creationParams?.get("url") as? String
        if (url != null && url != "") {
            if (url.startsWith("http")) {
                view.setNinePatchUrl(url) {
                    channel.invokeMethod("onLoaded", null)
                }
            } else {
                view.setNinePatchAsset(url) {
                    channel.invokeMethod("onLoaded", null)
                }
            }
        }
    }


    override fun getView(): View = view

    override fun dispose() {}
}